package com.controller;

import com.pojo.Files;
import com.pojo.Page;
import com.service.FileService;
import com.util.FileUtil;
import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@Controller
@RequestMapping("/file")
public class FileController {
    @Resource
    private FileService service;
    @RequestMapping("/upload")
    public String upload(MultipartFile uploadFile,HttpServletRequest request) throws IOException, SQLException {
        System.out.println("uploadFile="+uploadFile);
        byte[] buf = uploadFile.getBytes();
        System.out.println("文件长度"+buf.length);
        System.out.println("文件名getName="+uploadFile.getName());
        System.out.println("文件名getOriginalFileName="+uploadFile.getOriginalFilename());

        String originalFileName=uploadFile.getOriginalFilename();
        String time = FileUtil.createFileTimestamp();
        String fileUrl="D:\\IDEA\\upload\\"+originalFileName;
        System.out.println("fileUrl="+fileUrl);
        FileUtil.writeFileToUrl(uploadFile,fileUrl);

        Files files = new Files();
        files.setFileName(originalFileName);
        files.setFileUrl(fileUrl);
        files.setUserid(1);
        int flag = service.addFile(files);
        if(flag>0){
            request.setAttribute("info","success");
        }
        else{
            request.setAttribute("info","error");
        }
        return "html/Upload";
    }
    @RequestMapping("/findfile")
    @ResponseBody
    public Page findfile(int pageIndex) throws SQLException {
        Page page=new Page(5,pageIndex);
        List<Files>list=   service.findFiles(page.getStartIndex(),page.getEndIndex());
        int row=service.countAll(null);
        page.setResult(list,row);
        return page;
    }
    @RequestMapping("/download")
    public ResponseEntity<byte[]> download(int fileid) throws IOException {
        Files files =service.findById(fileid);
        System.out.println(fileid);
        String fileUrl = files.getFileUrl();
        String fileName = files.getFileName();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentDispositionFormData("attachment",fileName);
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);

        ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(new File(fileUrl)),headers, HttpStatus.CREATED);
        return entity;
    }
    @RequestMapping("/findfileByName")
    @ResponseBody
    public Page findfilebyname(int pageIndex,String fileName) throws SQLException{
        Page page=new Page(5,pageIndex);
        List<Files>list=   service.findFileByName(page.getStartIndex(),page.getEndIndex(),fileName);
        int row=service.countAll(fileName);
        page.setResult(list,row);
        return page;
    }
    @RequestMapping("/deletefile")
    @ResponseBody
    public String deleteFile(int fileId){
        int result=service.deleteFiles(fileId);
        if(result>0){
            return "{\"fig\":\"success\"}";
        }
        else{
            return "{\"fig\":\"error\"}";
        }
    }
}
